#!/bin/sh
. /etc/utils.sh

CONFIG_PATH=/var/config

trim() {
  local var="$1"
  var="${var#"${var%%[![:space:]]*}"}"
  var="${var%"${var##*[![:space:]]}"}"
  echo -n $var
}

hddserial() {
  local var
  var=$(hdparm -I "$sata_blkdev" | grep "Serial Number")
  trim "${var##*:}"
}

check_pairing() {
  attachedSerial=$(hddserial)
  pairedSerial=$(hnvram -qr PAIRED_DISK)
  echo "Attached HDD Serial:$attachedSerial, Paired HDD Serial:$pairedSerial"
  if [ "$attachedSerial" = "$pairedSerial" ]; then
    echo "   MATCH!"
  else
    if [ "$pairedSerial" = "" ]; then
      echo "   NO PAIR; PAIRING:$attachedSerial"
      hnvram -w PAIRED_DISK="$attachedSerial"
    else
      echo "   NO MATCH; NO MOUNT"
      echo 1 >/tmp/gpio/ledcontrol/hdd_bad_pair
      exit 0
    fi
  fi
}

FLAGS=noatime,barrier=1,data=writeback,noexec,nosuid,nodev,nohide_stale_gid=200
if is-storage-box; then
  FLAGS="${FLAGS},pin_block_bitmaps"
fi

case "$1" in
  start)
    failure_media=1
    failure_config=1

    sata_blkdev=$(find_sata_blkdev)
    if ! [ -e "$sata_blkdev" ]; then
      # This should only happen on recovery or NFS boots
      echo "No drive found; rescanning."
      wait-scsi

      # "ata1.00: both IDENTIFYs aborted, assuming NODEV"
      # This happens sometimes, make it rescan.
      for d in /sys/class/scsi_host/host*/scan; do
        [ -e "$d" ] && echo "0 0 0" >$d
      done
      sleep 5

      sata_blkdev=$(find_sata_blkdev)
    fi
    if [ -e "$sata_blkdev" ]; then
      (
        smartctl -A "$sata_blkdev"
        smartctl -t short "$sata_blkdev"
        sleep 60
        smartctl -l selftest "$sata_blkdev"
      ) 2>&1 | logos smartctl &
      # Disable power saving; not support by Seagate drive
      hdparm -I "$sata_blkdev" | grep -q ST4000 || hdparm -q -B255 "$sata_blkdev"
      check_pairing
      echo "Mounting SATA device."
      if [ -e "$sata_blkdev"1 ]; then
        tune2fs -m 0 "$sata_blkdev"1
        mount -o "$FLAGS" -t ext4 "$sata_blkdev"1 /var/media
        failure_media=$?

        if [ -e /var/media/.data ]; then
          echo "Interrupted factory reset detected. Cleaning up ..."
          rm -rf /var/media/*
          mv /var/media/.data /var/media/data
        fi

        if [ -e "$sata_blkdev"20 ]; then
          mount -o "$FLAGS,sync" -t ext4 "$sata_blkdev"20 "$CONFIG_PATH"
          failure_config=$?
        fi
      fi
    else
      echo "No drive found in Storage Box; failing."
    fi
    if [ "$failure_media" != 0 ] || [ "$failure_config" != 0 ]; then
      echo "HDD mounting error."
      echo 1 >/tmp/gpio/ledcontrol/hdd_mount_err
      exit 0
    fi
    # /config must be writeable by non-root
    chown bin.sys "$CONFIG_PATH"
    chmod 775 "$CONFIG_PATH"

    ;;
  stop)
    # Unmount SATA device
    echo "Unmounting SATA device"
    umount /var/media >/dev/null 2>&1
    umount "$CONFIG_PATH" >/dev/null 2>&1
    ;;
  powerdown)
    # Shutting off the drive reduces power and heat by another watt or so.
    if [ -n "$sata_blkdev" ]; then
      hdparm -S 5 -Y "$sata_blkdev"
    fi
    ;;
  restart|reload)
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|powerdown}"
    exit 1
esac
